home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 1 / LSD Compendium Deluxe 1.iso / a / programming / misc / ada1110b.lha / Examples / Dine / roombody.ada < prev    next >
Encoding:
Text File  |  1992-03-02  |  3.5 KB  |  106 lines

  1. WITH Windows;
  2. WITH Chop;
  3. WITH Phil;
  4. WITH Calendar;
  5. PRAGMA Elaborate(Phil);
  6. PACKAGE BODY Room IS
  7.  
  8. -- Body of Room package. The room contains a set of philosopher tasks
  9. -- and a set of windows in which philosophers report their states.
  10. -- The task Head_Waiter takes care of the window management.
  11. -- Head_Waiter also assigns chopsticks to philosophers. To avoid deadlock,
  12. -- this version of Head_Waiter commands four of the five philosophers to
  13. -- pick up their left sticks first; the fifth is told to pick up his\
  14. -- right stick first, which breaks the deadlock potential.
  15. -- Michael B. Feldman, The George Washington University, November 1990.
  16.  
  17.  
  18.   Phils:      ARRAY(Table_Type) OF Phil.Philosopher;
  19.   Phil_Windows: ARRAY(Table_Type) OF Windows.Window;
  20.  
  21.   TASK BODY Head_Waiter IS
  22.  
  23.     T : Integer;
  24.     Start_Time: Calendar.Time;
  25.  
  26.   BEGIN
  27.  
  28.     ACCEPT Open_The_Room;
  29.     Start_Time := Calendar.Clock;
  30.  
  31.     Windows.Open(Phil_Windows(1),1,23,7,30);
  32.     Windows.Borders(Phil_Windows(1),'+','|','-');
  33.     Windows.Title(Phil_Windows(1), "Eddy Dijkstra",'-');
  34.     Phils(1).Come_To_Life(1,1,2);
  35.  
  36.     Windows.Open(Phil_Windows(3),8,50,7,30);
  37.     Windows.Borders(Phil_Windows(3),'+','|','-');
  38.     Windows.Title(Phil_Windows(3), "Grady Booch",'-');
  39.     Phils(3).Come_To_Life(3,3,4);
  40.  
  41.     Windows.Open(Phil_Windows(2),8,2,7,30);
  42.     Windows.Borders(Phil_Windows(2),'+','|','-');
  43.     Windows.Title(Phil_Windows(2), "Putnam Texel",'-');
  44.     Phils(2).Come_To_Life(2,2,3);
  45.  
  46.     Windows.Open(Phil_Windows(5),15,41,7,30);
  47.     Windows.Borders(Phil_Windows(5),'+','|','-');
  48.     Windows.Title(Phil_Windows(5), "Bjarne Stroustrup",'-');
  49.     Phils(5).Come_To_Life(5,1,5);
  50.  
  51.     Windows.Open(Phil_Windows(4),15,8,7,30);
  52.     Windows.Borders(Phil_Windows(4),'+','|','-');
  53.     Windows.Title(Phil_Windows(4), "Jean Ichbiah",'-');
  54.     Phils(4).Come_To_Life(4,4,5);
  55.  
  56.     LOOP
  57.       SELECT
  58.         ACCEPT Report_State(Which_Phil: Table_Type;
  59.                          State: Phil.States;
  60.                          How_Long: Natural := 0) DO
  61.           T := Integer(Calendar."-"(Calendar.Clock,Start_Time));
  62.           Windows.Put_String(Phil_Windows(Which_Phil),
  63.             "T=" & Integer'Image(T) & " ");
  64.  
  65.           CASE State IS
  66.             WHEN Phil.Breathing =>
  67.               Windows.Put_String(Phil_Windows(Which_Phil), "Breathing...");
  68.               Windows.New_Line(Phil_Windows(Which_Phil));
  69.  
  70.             WHEN Phil.Thinking =>
  71.               Windows.Put_String(Phil_Windows(Which_Phil),
  72.                 "Thinking" & Integer'Image(How_Long) & " seconds.");
  73.               Windows.New_Line(Phil_Windows(Which_Phil));
  74.  
  75.             WHEN Phil.Eating =>
  76.               Windows.Put_String(Phil_Windows(Which_Phil),
  77.                 "Eating"   & Integer'Image(How_Long) & " seconds.");
  78.               Windows.New_Line(Phil_Windows(Which_Phil));
  79.  
  80.             WHEN Phil.Done_Eating =>
  81.               Windows.Put_String(Phil_Windows(Which_Phil), "Yum-yum (burp)");
  82.               Windows.New_Line(Phil_Windows(Which_Phil));
  83.  
  84.             WHEN Phil.Got_One_Stick =>
  85.               Windows.Put_String(Phil_Windows(Which_Phil),
  86.                 "First chopstick" & Integer'Image(How_Long));
  87.               Windows.New_Line(Phil_Windows(Which_Phil));
  88.  
  89.             WHEN Phil.Got_Other_Stick =>
  90.               Windows.Put_String(Phil_Windows(Which_Phil),
  91.                 "Second chopstick" & Integer'Image(How_Long));
  92.               Windows.New_Line(Phil_Windows(Which_Phil));
  93.  
  94.           END CASE;
  95.  
  96.          END Report_State;
  97.         OR
  98.           TERMINATE;
  99.         END SELECT;
  100.  
  101.       END LOOP;
  102.  
  103.     END Head_Waiter;
  104.  
  105. END Room;
  106.